package com.yu.crm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.domain.pojo.CrmArea;
import com.yu.crm.mapper.CrmAreaMapper;
import com.yxzz.common.exception.GeneralException;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.valid.ValidatorHelpers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 后台区域表 服务实现类
 * </p>
 *
 * @author ct
 * @since 2019-08-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmAreaService extends ServiceImpl<CrmAreaMapper, CrmArea> implements IService<CrmArea> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询所有区域信息
     * @return
     */
    public List<CrmArea> list(){
        try {
            QueryWrapper<CrmArea> queryWrapper=new QueryWrapper<>();
            return baseMapper.selectList(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new GeneralException("操作失败");
        }
    }

    /**
     * 新增区域名称
     */
    public void addCrmArea(String areaName){
        try {
            QueryWrapper<CrmArea> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("area_name",areaName);
            CrmArea crmArea1 = baseMapper.selectOne(queryWrapper);
            if(!DataHelpers.isNotEmpty(crmArea1)){
                CrmArea crmArea=new CrmArea();
                crmArea.setAreaName(areaName);

                ValidatorHelpers.validateDataSave(baseMapper.insert(crmArea));
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new GeneralException("操作失败");
        }
    }
}
